<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="###Java 常用加密算法(二)—对称加密(DES/3DES(TripleDES)/AES) 基于“对称密钥”的加密算法主要有DES、3DES（TripleDES）、AES、RC2、RC4、RC5和Blowfish等。">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="Java-常用加密算法(二)---对称加密(DES-3DES(TripleDES)-AES)">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/Java/Java-常用加密算法(二)---对称加密(DES-3DES(TripleDES)-AES)/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="###Java 常用加密算法(二)—对称加密(DES/3DES(TripleDES)/AES) 基于“对称密钥”的加密算法主要有DES、3DES（TripleDES）、AES、RC2、RC4、RC5和Blowfish等。">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2018-08-06T10:49:23.082Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java-常用加密算法(二)---对称加密(DES-3DES(TripleDES)-AES)">
<meta name="twitter:description" content="###Java 常用加密算法(二)—对称加密(DES/3DES(TripleDES)/AES) 基于“对称密钥”的加密算法主要有DES、3DES（TripleDES）、AES、RC2、RC4、RC5和Blowfish等。">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/Java/Java-常用加密算法(二)---对称加密(DES-3DES(TripleDES)-AES)/"/>





  <title>Java-常用加密算法(二)---对称加密(DES-3DES(TripleDES)-AES) | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/Java/Java-常用加密算法(二)---对称加密(DES-3DES(TripleDES)-AES)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Java-常用加密算法(二)---对称加密(DES-3DES(TripleDES)-AES)</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:39+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>###Java 常用加密算法(二)—对称加密(DES/3DES(TripleDES)/AES)</p>
<p><strong>基于“<a href="http://baike.baidu.com/view/1145164.htm" target="_blank" rel="noopener">对称密钥</a>”的加密算法主要有DES、3DES（TripleDES）、AES、RC2、RC4、RC5和Blowfish等。</strong><br><a id="more"></a></p>
<p><strong><em>代码中用到的Base64与BouncyCastleProvider的jar包可在本文末尾下载源码，从源码获取jar包。</em></strong></p>
<ol>
<li><p>DES<br> DES算法全称为Data Encryption Standard，即数据加密算法，它是IBM公司于1975年研究成功并公开发表的。DES算法的入口参数有三个：Key、Data、Mode。其中Key为8个字节共64位，是DES算法的工作密钥；Data也为8个字节64位，是要被加密或被解密的数据；Mode为DES的工作方式,有两种：加密或解密。<br> 五种分组模式：EBC模式、CBC模式、CFB模式、OFB模式、CTR模式。<br>其中CTR 模式被广泛用于 ATM 网络安全和 IPSec应用中，相对于其它模式而言，CRT模式具有如下特点：<br>■硬件效率：允许同时处理多块明文 / 密文。<br>■ 软件效率：允许并行计算，可以很好地利用 CPU 流水等并行技术。<br>■ 预处理：算法和加密盒的输出不依靠明文和密文的输入，因此如果有足够的保证安全的存储器，加密算法将仅仅是一系列异或运算，这将极大地提高吞吐量。<br>■ 随机访问：第 i 块密文的解密不依赖于第 i-1 块密文，提供很高的随机访问能力<br>■ 可证明的安全性：能够证明 CTR 至少和其他模式一样安全（CBC, CFB, OFB, …）<br>■ 简单性：与其它模式不同，CTR模式仅要求实现加密算法，但不要求实现解密算法。对于 AES 等加/解密本质上不同的算法来说，这种简化是巨大的。<br>■ 无填充，可以高效地作为流式加密使用。<br>在Java进行DES、3DES和AES三种对称加密算法时，常采用的是NoPadding（不填充）、Zeros填充（0填充）、PKCS5Padding填充。<br>示例：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * DES加密工具类</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class DESUtil &#123;</span><br><span class="line">	/**</span><br><span class="line">	 * 算法名称</span><br><span class="line">	 */</span><br><span class="line">	public static final String KEY_ALGORITHM = &quot;DES&quot;;</span><br><span class="line">	/**</span><br><span class="line">	 * 算法名称/加密模式/填充方式</span><br><span class="line">	 * DES的四种工作模式：ECB(电子密码本)、CBC(加密分组链接)、</span><br><span class="line">	 * CFB(加密反馈模式)、OFB(输出反馈)</span><br><span class="line">	 * 当前无填充的情况：</span><br><span class="line">	 * 		加密数据必须为8的倍数，密钥输入必须为16的倍数</span><br><span class="line">	 *  使用</span><br><span class="line">		String source = &quot;abcdefgh&quot;;// 为8位的倍数</span><br><span class="line">		String key = &quot;A1B2C3D4E5F60708&quot;;// 为16位的倍数</span><br><span class="line">		</span><br><span class="line">		String encryptData = DESUtil.encrypt(source, key);</span><br><span class="line">		System.out.println(&quot;加密后: &quot; + encryptData);</span><br><span class="line">		</span><br><span class="line">		String decryptData = DESUtil.decrypt(encryptData, key);</span><br><span class="line">		System.out.println(&quot;解密后: &quot; + decryptData);</span><br><span class="line">	 */</span><br><span class="line">	public static final String CIPHER_ALGORITHM_DES_ECB_NOPADDING = &quot;DES/ECB/NoPadding&quot;;</span><br><span class="line">	/**</span><br><span class="line">	 * DES/CBC/PKCS5Padding</span><br><span class="line">	 * PKCS5Padding填充时:</span><br><span class="line">	 * 		加密数据无位数控制，密钥输入必须为16的倍数</span><br><span class="line">	 * 	使用</span><br><span class="line">		String source = &quot;abcdefgh&quot;;// 无需控制位数</span><br><span class="line">		String key = &quot;A1B2C3D4E5F60708&quot;;// 为16位的倍数</span><br><span class="line">		</span><br><span class="line">		String encryptData = DESUtil.encrypt(source, key);</span><br><span class="line">		System.out.println(&quot;加密后: &quot; + encryptData);</span><br><span class="line">		</span><br><span class="line">		String decryptData = DESUtil.decrypt(encryptData, key);</span><br><span class="line">		System.out.println(&quot;解密后: &quot; + decryptData);</span><br><span class="line">		</span><br><span class="line">	 */</span><br><span class="line">	public static final String CIPHER_ALGORITHM_DES_ECB_PKCS5_PADDING = &quot;DES/ECB/PKCS5Padding&quot;;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 加密数据</span><br><span class="line">	 * @param data 待加密的数据--8位</span><br><span class="line">	 * @param key 密钥--16位</span><br><span class="line">	 * @return 加密后的数据</span><br><span class="line">	 */</span><br><span class="line">	public static String encrypt(String data, String key) &#123;</span><br><span class="line">		try &#123;</span><br><span class="line">			// 获得一个密钥</span><br><span class="line">			Key deskey = keyGenerator(key);</span><br><span class="line">			// 实例化一个Cipher(密码)对象，用于完成加密操作</span><br><span class="line">			Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_DES_ECB_PKCS5_PADDING);</span><br><span class="line">			SecureRandom random = new SecureRandom();</span><br><span class="line">			// 初始化Cipher对象，设置为加密模式</span><br><span class="line">			cipher.init(Cipher.ENCRYPT_MODE, deskey, random);</span><br><span class="line">			byte[] bytes = data.getBytes();</span><br><span class="line">			// 执行加密操作</span><br><span class="line">			byte[] results = cipher.doFinal(bytes);</span><br><span class="line">			return Base64.encodeBase64String(results);</span><br><span class="line">		&#125; catch (InvalidKeyException e) &#123;</span><br><span class="line">			return &quot;无效KEY&quot;; // 无效KEY</span><br><span class="line">		&#125; catch (NoSuchAlgorithmException e) &#123;</span><br><span class="line">			return &quot;无效算法名称&quot;; // 无效算法名称</span><br><span class="line">		&#125; catch (InvalidKeySpecException e) &#123;</span><br><span class="line">			return &quot;无效KeySpec&quot;; // 无效KeySpec</span><br><span class="line">		&#125; catch (NoSuchPaddingException e) &#123;</span><br><span class="line">			return &quot;无效算法名称&quot;; // 无效算法名称</span><br><span class="line">		&#125; catch (IllegalBlockSizeException e) &#123;</span><br><span class="line">			return &quot;无效字节&quot;; // 无效字节</span><br><span class="line">		&#125; catch (BadPaddingException e) &#123;</span><br><span class="line">			return &quot;解析异常&quot;; // 解析异常</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	/**</span><br><span class="line">	 * 解密数据</span><br><span class="line">	 * @param data 待解密数据</span><br><span class="line">	 * @param key 密钥</span><br><span class="line">	 * @return 解密后的数据</span><br><span class="line">	 */</span><br><span class="line">	public static String decrypt(String data, String key) &#123;</span><br><span class="line">		try &#123;</span><br><span class="line">			Key desKey = keyGenerator(key);</span><br><span class="line">			Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_DES_ECB_PKCS5_PADDING);</span><br><span class="line">			// 初始化Cipher对象，设置为解密模式</span><br><span class="line">			cipher.init(Cipher.DECRYPT_MODE, desKey);</span><br><span class="line">			// 执行解密操作</span><br><span class="line">			byte[] decBytes = cipher.doFinal(Base64.decodeBase64(data));</span><br><span class="line">			return new String(decBytes,&quot;UTF-8&quot;);</span><br><span class="line">		&#125; catch (InvalidKeyException e) &#123;</span><br><span class="line">			return &quot;无效KEY&quot;; // 无效KEY</span><br><span class="line">		&#125; catch (NoSuchAlgorithmException e) &#123;</span><br><span class="line">			return &quot;无效算法名称&quot;; // 无效算法名称</span><br><span class="line">		&#125; catch (InvalidKeySpecException e) &#123;</span><br><span class="line">			return &quot;无效KeySpec&quot;; // 无效KeySpec</span><br><span class="line">		&#125; catch (NoSuchPaddingException e) &#123;</span><br><span class="line">			return &quot;无效算法名称&quot;; // 无效算法名称</span><br><span class="line">		&#125; catch (IllegalBlockSizeException e) &#123;</span><br><span class="line">			return &quot;无效字节&quot;; // 无效字节</span><br><span class="line">		&#125; catch (BadPaddingException e) &#123;</span><br><span class="line">			return &quot;解析异常&quot;; // 解析异常</span><br><span class="line">		&#125; catch (UnsupportedEncodingException e) &#123;</span><br><span class="line">			return &quot;编码异常&quot;; // 编码异常</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 生成密钥key对象</span><br><span class="line">	 * @param key 密钥字符串</span><br><span class="line">	 * @return 密钥对象</span><br><span class="line">	 * @throws InvalidKeyException 无效的key</span><br><span class="line">	 * @throws NoSuchAlgorithmException 算法名称未发现</span><br><span class="line">	 * @throws InvalidKeySpecException 无效的KeySpec</span><br><span class="line">	 */</span><br><span class="line">	private static Key keyGenerator(String key) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException &#123;</span><br><span class="line">		byte[] input = hexString2Bytes(key);</span><br><span class="line">		DESKeySpec desKey = new DESKeySpec(input);</span><br><span class="line">		// 创建一个密钥工厂，然后用它把DESKeySpec转化</span><br><span class="line">		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);</span><br><span class="line">		// 获得一个密钥</span><br><span class="line">		SecretKey secretKey = keyFactory.generateSecret(desKey);</span><br><span class="line">		return secretKey;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	/**</span><br><span class="line">	 * 从十六进制字符串到字节数组转化</span><br><span class="line">	 * @param key 密钥</span><br><span class="line">	 */</span><br><span class="line">	private static byte[] hexString2Bytes(String key) &#123;</span><br><span class="line">		byte[] b = new byte[key.length()/2];</span><br><span class="line">		int j = 0;</span><br><span class="line">		for (int i = 0; i &lt; b.length; i++) &#123;</span><br><span class="line">			char c0 = key.charAt(j++);</span><br><span class="line">			char c1 = key.charAt(j++);</span><br><span class="line">			// c0做b[i]的高字节，c1做低字节</span><br><span class="line">			b[i] = (byte) ((parse(c0)&lt;&lt;4)|parse(c1));</span><br><span class="line">		&#125;</span><br><span class="line">		return b;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	/**</span><br><span class="line">	 * 将字符转换为int值</span><br><span class="line">	 * @param c 要转化的字符</span><br><span class="line">	 * @return ASCII码值</span><br><span class="line">	 */</span><br><span class="line">	private static int parse(char c) &#123;</span><br><span class="line">		if (c &gt;= &apos;a&apos;) &#123;</span><br><span class="line">			return (c - &apos;a&apos; + 10) &amp; 0x0f;</span><br><span class="line">		&#125;</span><br><span class="line">		if (c &gt;= &apos;A&apos;) &#123;</span><br><span class="line">			return (c - &apos;A&apos; + 10) &amp; 0x0f;</span><br><span class="line">		&#125;</span><br><span class="line">		return (c - &apos;0&apos;) &amp; 0x0f;</span><br><span class="line">	&#125;	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>3-DES<br>3DES（或称为Triple DES）是三重<a href="http://baike.baidu.com/view/878529.htm" target="_blank" rel="noopener">数据加密算法</a>（TDEA，Triple Data Encryption Algorithm）块密码的通称。它相当于是对每个<a href="http://baike.baidu.com/view/702806.htm" target="_blank" rel="noopener">数据块</a>应用三次DES<a href="http://baike.baidu.com/view/155969.htm" target="_blank" rel="noopener">加密算法</a>。由于计算机运算能力的增强，原版DES密码的<a href="http://baike.baidu.com/view/934.htm" target="_blank" rel="noopener">密钥</a>长度变得容易被暴力破解；3DES即是设计用来提供一种相对简单的方法，即通过增加DES的密钥长度来避免类似的攻击，而不是设计一种全新的块<a href="http://baike.baidu.com/view/8790.htm" target="_blank" rel="noopener">密码算法</a>。<br>示例：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 3-DES加密工具类</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class ThreeDESUtil &#123;</span><br><span class="line">	/**</span><br><span class="line">	 * 算法名称</span><br><span class="line">	 */</span><br><span class="line">	public static final String KEY_ALGORITHM = &quot;desede&quot;;</span><br><span class="line">	/**</span><br><span class="line">	 * 算法名称/加密模式/填充方式</span><br><span class="line">	 * </span><br><span class="line">	 	String source = &quot;abcdefgh&quot;; // 无填充情况下，长度必须为8的倍数</span><br><span class="line">		String key = &quot;6C4E60E55552386C759569836DC0F83869836DC0F838C0F7&quot;;// 长度必须大于等于48</span><br><span class="line">		System.out.println(key.length());</span><br><span class="line">		String encryptData = ThreeDESUtil.tDesEncryptCBC(source, key);</span><br><span class="line">		System.out.println(&quot;加密后: &quot; + encryptData);</span><br><span class="line">		</span><br><span class="line">		String decryptData = ThreeDESUtil.tDesDecryptCBC(encryptData, key);</span><br><span class="line">		System.out.println(&quot;解密后: &quot; + decryptData);	</span><br><span class="line">	 */</span><br><span class="line">	public static final String CIPHER_ALGORITHM = &quot;desede/CBC/NoPadding&quot;;</span><br><span class="line">	/**</span><br><span class="line">	 * IvParameterSpec参数</span><br><span class="line">	 */</span><br><span class="line">	public static final byte[] keyiv = &#123; 1, 2, 3, 4, 5, 6, 7, 8 &#125;;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * CBC加密</span><br><span class="line">	 * @param data 明文</span><br><span class="line">	 * @param key 密钥</span><br><span class="line">	 * @return Base64编码的密文</span><br><span class="line">	 */</span><br><span class="line">	public static String tDesEncryptCBC(String data, String key)&#123;</span><br><span class="line">		try &#123;</span><br><span class="line">			// 添加一个安全提供者</span><br><span class="line">			Security.addProvider(new BouncyCastleProvider());</span><br><span class="line">			// 获得密钥</span><br><span class="line">			Key desKey = keyGenerator(key);</span><br><span class="line">			// 获取密码实例</span><br><span class="line">			Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);</span><br><span class="line">			IvParameterSpec ips = new IvParameterSpec(keyiv);</span><br><span class="line">			// 初始化密码</span><br><span class="line">			cipher.init(Cipher.ENCRYPT_MODE, desKey, ips);</span><br><span class="line">			// 执行加密</span><br><span class="line">			byte[] bytes = cipher.doFinal(data.getBytes(&quot;UTF-8&quot;));</span><br><span class="line">			return Base64.encodeBase64String(bytes);</span><br><span class="line">		&#125; catch (InvalidKeyException e) &#123;</span><br><span class="line">			return &quot;无效KEY&quot;; // 无效KEY</span><br><span class="line">		&#125; catch (NoSuchAlgorithmException e) &#123;</span><br><span class="line">			return &quot;无效算法名称&quot;; // 无效算法名称</span><br><span class="line">		&#125; catch (InvalidKeySpecException e) &#123;</span><br><span class="line">			return &quot;无效KeySpec&quot;; // 无效KeySpec</span><br><span class="line">		&#125; catch (NoSuchPaddingException e) &#123;</span><br><span class="line">			return &quot;无效算法名称&quot;; // 无效算法名称</span><br><span class="line">		&#125; catch (IllegalBlockSizeException e) &#123;</span><br><span class="line">			return &quot;无效字节&quot;; // 无效字节</span><br><span class="line">		&#125; catch (BadPaddingException e) &#123;</span><br><span class="line">			return &quot;解析异常&quot;; // 解析异常</span><br><span class="line">		&#125; catch (UnsupportedEncodingException e) &#123;</span><br><span class="line">			return &quot;编码异常&quot;; // 编码异常</span><br><span class="line">		&#125; catch (InvalidAlgorithmParameterException e) &#123;</span><br><span class="line">			return &quot;摘要参数异常&quot;; // 摘要参数异常</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * CBC解密</span><br><span class="line">	 * @param data Base64编码的密文</span><br><span class="line">	 * @param key 密钥</span><br><span class="line">	 * @return</span><br><span class="line">	 */</span><br><span class="line">	public static String tDesDecryptCBC(String data, String key) &#123;</span><br><span class="line">		try &#123;</span><br><span class="line">			Key desKey = keyGenerator(key);</span><br><span class="line">			Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);</span><br><span class="line">			IvParameterSpec ips = new IvParameterSpec(keyiv);</span><br><span class="line">			cipher.init(Cipher.DECRYPT_MODE, desKey, ips);</span><br><span class="line">			byte[] bytes = cipher.doFinal(Base64.decodeBase64(data));</span><br><span class="line">			return new String(bytes, &quot;UTF-8&quot;);</span><br><span class="line">		&#125; catch (InvalidKeyException e) &#123;</span><br><span class="line">			return &quot;无效KEY&quot;; // 无效KEY</span><br><span class="line">		&#125; catch (NoSuchAlgorithmException e) &#123;</span><br><span class="line">			return &quot;无效算法名称&quot;; // 无效算法名称</span><br><span class="line">		&#125; catch (InvalidKeySpecException e) &#123;</span><br><span class="line">			return &quot;无效KeySpec&quot;; // 无效KeySpec</span><br><span class="line">		&#125; catch (NoSuchPaddingException e) &#123;</span><br><span class="line">			return &quot;无效算法名称&quot;; // 无效算法名称</span><br><span class="line">		&#125; catch (IllegalBlockSizeException e) &#123;</span><br><span class="line">			return &quot;无效字节&quot;; // 无效字节</span><br><span class="line">		&#125; catch (BadPaddingException e) &#123;</span><br><span class="line">			return &quot;解析异常&quot;; // 解析异常</span><br><span class="line">		&#125; catch (UnsupportedEncodingException e) &#123;</span><br><span class="line">			return &quot;编码异常&quot;; // 编码异常</span><br><span class="line">		&#125; catch (InvalidAlgorithmParameterException e) &#123;</span><br><span class="line">			return &quot;摘要参数异常&quot;; // 摘要参数异常</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 生成密钥key对象</span><br><span class="line">	 * @param key 密钥字符串</span><br><span class="line">	 * @return 密钥对象</span><br><span class="line">	 * @throws InvalidKeyException 无效的key</span><br><span class="line">	 * @throws NoSuchAlgorithmException 算法名称未发现</span><br><span class="line">	 * @throws InvalidKeySpecException 无效的KeySpec</span><br><span class="line">	 */</span><br><span class="line">	private static Key keyGenerator(String key) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException &#123;</span><br><span class="line">		byte[] input = hexString2Bytes(key);</span><br><span class="line">		DESedeKeySpec desKey = new DESedeKeySpec(input);</span><br><span class="line">		// 创建一个密钥工厂，然后用它把DESKeySpec转化</span><br><span class="line">		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);</span><br><span class="line">		// 获得一个密钥</span><br><span class="line">		SecretKey secretKey = keyFactory.generateSecret(desKey);</span><br><span class="line">		return secretKey;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	/**</span><br><span class="line">	 * 从十六进制字符串到字节数组转化</span><br><span class="line">	 * @param key 密钥</span><br><span class="line">	 */</span><br><span class="line">	private static byte[] hexString2Bytes(String key) &#123;</span><br><span class="line">		byte[] b = new byte[key.length()/2];</span><br><span class="line">		int j = 0;</span><br><span class="line">		for (int i = 0; i &lt; b.length; i++) &#123;</span><br><span class="line">			char c0 = key.charAt(j++);</span><br><span class="line">			char c1 = key.charAt(j++);</span><br><span class="line">			// c0做b[i]的高字节，c1做低字节</span><br><span class="line">			b[i] = (byte) ((parse(c0)&lt;&lt;4)|parse(c1));</span><br><span class="line">		&#125;</span><br><span class="line">		return b;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	/**</span><br><span class="line">	 * 将字符转换为int值</span><br><span class="line">	 * @param c 要转化的字符</span><br><span class="line">	 * @return ASCII码值</span><br><span class="line">	 */</span><br><span class="line">	private static int parse(char c) &#123;</span><br><span class="line">		if (c &gt;= &apos;a&apos;) &#123;</span><br><span class="line">			return (c - &apos;a&apos; + 10) &amp; 0x0f;</span><br><span class="line">		&#125;</span><br><span class="line">		if (c &gt;= &apos;A&apos;) &#123;</span><br><span class="line">			return (c - &apos;A&apos; + 10) &amp; 0x0f;</span><br><span class="line">		&#125;</span><br><span class="line">		return (c - &apos;0&apos;) &amp; 0x0f;</span><br><span class="line">	&#125;	</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>AES加密<br>AES是分组密钥，算法输入128位数据，密钥长度也是128位。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * AES加密工具类</span><br><span class="line"> * &lt;p&gt;</span><br><span class="line"> * 使用：</span><br><span class="line"> * String source = &quot;mazaiting&quot;;</span><br><span class="line"> * String key = &quot;123456&quot;;</span><br><span class="line"> * &lt;p&gt;</span><br><span class="line"> * String encryptString = AesUtil.encrypt(source, key);</span><br><span class="line"> * System.out.println(&quot;加密后: &quot; + encryptString);</span><br><span class="line"> * &lt;p&gt;</span><br><span class="line"> * String decryptString = AesUtil.decrypt(encryptString, key);</span><br><span class="line"> * System.out.println(&quot;解密后: &quot; + decryptString);</span><br><span class="line"> * 需要依赖：compile &apos;commons-codec:commons-codec:1.11&apos;</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class AesUtil &#123;</span><br><span class="line">  /**</span><br><span class="line">   * 算法名称</span><br><span class="line">   */</span><br><span class="line">  private static final String KEY_ALGORITHM = &quot;AES&quot;;</span><br><span class="line">  /**</span><br><span class="line">   * 算法</span><br><span class="line">   */</span><br><span class="line">  private static final String CIPHER_ALGORITHM = &quot;AES&quot;;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 加密数据</span><br><span class="line">   *</span><br><span class="line">   * @param data 待加密内容</span><br><span class="line">   * @param key  加密的密钥</span><br><span class="line">   * @return 加密后的数据</span><br><span class="line">   */</span><br><span class="line">  public static String encrypt(String data, String key) &#123;</span><br><span class="line">    try &#123;</span><br><span class="line">      // 获得密钥</span><br><span class="line">      Key deskey = keyGenerator(key);</span><br><span class="line">      // 实例化一个密码对象</span><br><span class="line">      Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);</span><br><span class="line">      // 密码初始化</span><br><span class="line">      cipher.init(Cipher.ENCRYPT_MODE, deskey);</span><br><span class="line">      // 执行加密</span><br><span class="line">      byte[] bytes = cipher.doFinal(data.getBytes(&quot;UTF-8&quot;));</span><br><span class="line">      // 返回Base64编码后的字符串</span><br><span class="line">      return Base64.encodeBase64String(bytes);</span><br><span class="line">    &#125; catch (NoSuchAlgorithmException e) &#123;</span><br><span class="line">      return &quot;加密名称异常&quot;; // 获取加密名称异常</span><br><span class="line">    &#125; catch (UnsupportedEncodingException e) &#123;</span><br><span class="line">      return &quot;未知编码格式&quot;; // 未知编码格式</span><br><span class="line">    &#125; catch (InvalidKeyException e) &#123;</span><br><span class="line">      return &quot;无效Key&quot;; // 无效Key</span><br><span class="line">    &#125; catch (NoSuchPaddingException e) &#123;</span><br><span class="line">      return &quot;无效密码算法&quot;; // 无效密码算法</span><br><span class="line">    &#125; catch (IllegalBlockSizeException e) &#123;</span><br><span class="line">      return &quot;无效字节&quot;; // 无效字节</span><br><span class="line">    &#125; catch (BadPaddingException e) &#123;</span><br><span class="line">      return &quot;解析异常&quot;; // 解析异常</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 解密数据</span><br><span class="line">   *</span><br><span class="line">   * @param data 待解密的内容</span><br><span class="line">   * @param key  解密的密钥</span><br><span class="line">   * @return 解密后的文字</span><br><span class="line">   */</span><br><span class="line">  public static String decrypt(String data, String key) &#123;</span><br><span class="line">    try &#123;</span><br><span class="line">      // 生成密钥</span><br><span class="line">      Key kGen = keyGenerator(key);</span><br><span class="line">      // 实例化密码对象</span><br><span class="line">      Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);</span><br><span class="line">      // 初始化密码对象</span><br><span class="line">      cipher.init(Cipher.DECRYPT_MODE, kGen);</span><br><span class="line">      // 执行解密</span><br><span class="line">      byte[] bytes = cipher.doFinal(Base64.decodeBase64(data));</span><br><span class="line">      // 返回解密后的字符串</span><br><span class="line">      return new String(bytes, &quot;UTF-8&quot;);</span><br><span class="line">    &#125; catch (NoSuchAlgorithmException e) &#123;</span><br><span class="line">      return &quot;加密名称异常&quot;; // 获取加密名称异常</span><br><span class="line">    &#125; catch (UnsupportedEncodingException e) &#123;</span><br><span class="line">      return &quot;未知编码格式&quot;; // 未知编码格式</span><br><span class="line">    &#125; catch (InvalidKeyException e) &#123;</span><br><span class="line">      return &quot;无效Key&quot;; // 无效Key</span><br><span class="line">    &#125; catch (NoSuchPaddingException e) &#123;</span><br><span class="line">      return &quot;无效密码算法&quot;; // 无效密码算法</span><br><span class="line">    &#125; catch (IllegalBlockSizeException e) &#123;</span><br><span class="line">      return &quot;无效字节&quot;; // 无效字节</span><br><span class="line">    &#125; catch (BadPaddingException e) &#123;</span><br><span class="line">      return &quot;解析异常&quot;; // 解析异常</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 获取密钥</span><br><span class="line">   *</span><br><span class="line">   * @param key 密钥字符串</span><br><span class="line">   * @return 返回一个密钥</span><br><span class="line">   * @throws NoSuchAlgorithmException</span><br><span class="line">   * @throws UnsupportedEncodingException</span><br><span class="line">   */</span><br><span class="line">  private static Key keyGenerator(String key) throws NoSuchAlgorithmException, UnsupportedEncodingException &#123;</span><br><span class="line">    KeyGenerator kGen = KeyGenerator.getInstance(KEY_ALGORITHM);</span><br><span class="line">    kGen.init(128, new SecureRandom(hexString2Bytes(key)));</span><br><span class="line">    SecretKey secretKey = kGen.generateKey();</span><br><span class="line">    byte[] encoded = secretKey.getEncoded();</span><br><span class="line">    return new SecretKeySpec(encoded, KEY_ALGORITHM);</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 从十六进制字符串到字节数组转化</span><br><span class="line">   *</span><br><span class="line">   * @param key 密钥</span><br><span class="line">   */</span><br><span class="line">  private static byte[] hexString2Bytes(String key) &#123;</span><br><span class="line">    byte[] b = new byte[key.length() / 2];</span><br><span class="line">    int j = 0;</span><br><span class="line">    for (int i = 0; i &lt; b.length; i++) &#123;</span><br><span class="line">      char c0 = key.charAt(j++);</span><br><span class="line">      char c1 = key.charAt(j++);</span><br><span class="line">      // c0做b[i]的高字节，c1做低字节</span><br><span class="line">      b[i] = (byte) ((parse(c0) &lt;&lt; 4) | parse(c1));</span><br><span class="line">    &#125;</span><br><span class="line">    return b;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 将字符转换为int值</span><br><span class="line">   *</span><br><span class="line">   * @param c 要转化的字符</span><br><span class="line">   * @return ASCII码值</span><br><span class="line">   */</span><br><span class="line">  private static int parse(char c) &#123;</span><br><span class="line">    if (c &gt;= &apos;a&apos;) &#123;</span><br><span class="line">      return (c - &apos;a&apos; + 10) &amp; 0x0f;</span><br><span class="line">    &#125;</span><br><span class="line">    if (c &gt;= &apos;A&apos;) &#123;</span><br><span class="line">      return (c - &apos;A&apos; + 10) &amp; 0x0f;</span><br><span class="line">    &#125;</span><br><span class="line">    return (c - &apos;0&apos;) &amp; 0x0f;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p><a href="https://gitee.com/mazaiting/codes/1grqe7atchwpk92xz0v6u93" target="_blank" rel="noopener">代码下载</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/Java/Java-常用加密算法(一)---单向加密算法(MD5-SHA)/" rel="next" title="Java-常用加密算法(一)---单向加密算法(MD5-SHA)">
                <i class="fa fa-chevron-left"></i> Java-常用加密算法(一)---单向加密算法(MD5-SHA)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/Java/Java-线程池(Executor)/" rel="prev" title="Java-线程池(Executor)">
                Java-线程池(Executor) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
